//
//  ActiveViewController.m
//  豆瓣zlw
//
//  Created by lanou3g on 16/1/7.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "ActiveViewController.h"
#import "ActiveModel.h"
#import "ActiveTableViewCell.h"
#import <UIImageView+WebCache.h>
@interface ActiveViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *arrAllData;
}

@end

@implementation ActiveViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.backgroundColor=[UIColor greenColor];
    //遵守协议
    self.ActiveTableView.delegate=self;
    self.ActiveTableView.dataSource=self;
    [self getDatas];
    
}
-(void)getDatas
{
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:ACTIVEURL]];
    NSURLSession *session=[NSURLSession sharedSession];
    NSURLSessionTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data)
        {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            arrAllData=[NSMutableArray array];
            for ( NSDictionary *dic in dict[@"events"])
            {
                ActiveModel *model=[[ActiveModel alloc] init];
                
                
                [model setValuesForKeysWithDictionary:dic[@"owner"]];
              
                [model setValuesForKeysWithDictionary:dic];
                [arrAllData addObject:model];
            }
        }
        [self performSelectorOnMainThread:@selector(mainAction) withObject:nil waitUntilDone:nil];
    }];
    [task resume];
    
}
-(void)mainAction
{
    [self.HUD hide:YES];
    [self.ActiveTableView reloadData];
}

//代理必须实现的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrAllData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActiveTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Active_id" forIndexPath:indexPath];
    ActiveModel *model=arrAllData[indexPath.row];
    cell.Titlelable.text=model.title;
    cell.begin_timLable.text=model.begin_time;
    cell.end_timeLable.text=model.end_time;
    cell.AddressLable.text=model.address;
    cell.NameLable.text=model.name;
    cell.category_nameLable.text=model.category_name;
    cell.wisher_countLable.text=[NSString stringWithFormat:@"%@",model.wisher_count];
    cell.partipant_countLable.text=[NSString stringWithFormat:@"%@",model.participant_count];
    
    [cell.imV sd_setImageWithURL:[NSURL URLWithString:model.image] ];
   
    cell.imV.backgroundColor=[UIColor greenColor];
    return cell;
}

#pragma mark-----转到详情页面---------------
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   //取到目标控制器
    id des=segue.destinationViewController;
    //根据sender找到从哪里跳过去
    ActiveTableViewCell *cell=(ActiveTableViewCell *)sender;
    NSIndexPath *path=[self.ActiveTableView indexPathForCell:cell ];
    //用KVC给目标控制器的属性赋值,把值传过去
    [des setValue:arrAllData[path.row] forKey:@"getmodel"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
